package com.example.RuanJianKeShe.Service.student;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.RuanJianKeShe.Bean.SC;
import com.example.RuanJianKeShe.Dao.impl.StuCourseDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

@WebServlet("/addSelectResultCourse")
public class addSelectResultCourse extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        String jsonData = req.getParameter("jsonData");
        HttpSession session = req.getSession();
        int sno = Integer.parseInt(session.getAttribute("mySno").toString());
        JSONObject jsonObject = JSONObject.parseObject(jsonData);
        String resultList = jsonObject.getString("resultList");
        System.out.println(jsonObject.getString("msg"));
        JSONArray list = JSONObject.parseArray(resultList);
        SC sc ;
        StuCourseDaoImpl stuCourseDao = new StuCourseDaoImpl();
        for (int i=0;i< list.size();i++){
            int cno = Integer.parseInt(list.get(i).toString());
            sc = new SC();
            sc.setSno(sno);
            sc.setCno(cno);
            Boolean flag = stuCourseDao.addStuCourse(sc);
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("info","添加成功");
        String jsonString = JSON.toJSONString(map);
        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
